package club.caihappy.store.service.impl;

import club.caihappy.store.entity.Address;
import club.caihappy.store.entity.Order;
import club.caihappy.store.entity.OrderItem;
import club.caihappy.store.mapper.AddressMapper;
import club.caihappy.store.mapper.OrderMapper;
import club.caihappy.store.service.IAddressService;
import club.caihappy.store.service.ICartService;
import club.caihappy.store.service.IDistrictService;
import club.caihappy.store.service.IOrderService;
import club.caihappy.store.service.ex.*;
import club.caihappy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 收货地址模块业务层的实现类
 */
@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private ICartService cartService;


    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cids) {
        List<CartVO> list = cartService.getVOByCid(uid, cids);
        //计算商品的总价
        Long totalPice = 0L;
        for (CartVO cartVO : list) {
            totalPice += cartVO.getRealPrice() * cartVO.getNum();
        }
        Address address = addressService.getByAid(aid, uid);
        Date date = new Date();
        Order order = new Order();
        //收回地址数据
        order.setUid(uid);
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //支付,总价
        order.setTotalPrice(totalPice);
        order.setStatus(0);
        order.setOrderTime(date);
        //日志
        order.setCreatedTime(date);
        order.setCreatedUser(username);
        order.setModifiedTime(date);
        order.setModifiedUser(username);

        Integer rows = orderMapper.insertOrder(order);
        if (rows != 1) {
            throw new InsertException("创建订单时发生未知错误");
        }

        //创建订单详细项
        for (CartVO cartVO : list) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOid(order.getOid());
            orderItem.setPid(cartVO.getPid());
            orderItem.setTitle(cartVO.getTitle());
            orderItem.setImage(cartVO.getImage());
            orderItem.setPrice(cartVO.getRealPrice());
            orderItem.setNum(cartVO.getNum());
            //日志字段
            orderItem.setCreatedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedTime(new Date());
            orderItem.setModifiedUser(username);
            Boolean flag = orderMapper.insertOrderItem(orderItem);
            if (!flag) {
                throw new InsertException("添加订单项时发生未知错误");
            }
        }

        return order;
    }
}
